from django.db import models

# Create your models here.

TYPE_CHOICES = {
    'normal': 'Traditional',
    'io':'I/O Interactive',
    'func':'Functional Interactive',
    'spj':'Special Judge',
    'text':'Submit answer',
}

STATUS_CHOICES = {
    # Basic Status
    'WJ' :'Pending',
    'JU' :'Judging',
    'AC' :'Accepted',
    'WA' :'Wrong Answer',
    'TLE': 'Time Limit Exceeded',
    'MLE': 'Memory Limit Exceeded',
    'RE' :'Runtime Error',
    'CE' :'Complication Error',
    'SE' :'SystemError',
    'UAC':'Unaccepted',
    'RJ' :'Rejected'
}

class Problem(models.Model):
    problem=models.CharField(verbose_name="problem",max_length=20)
    type=models.CharField(verbose_name="type",default="normal",max_length=20)
    class Meta:
        verbose_name = 'problem'
        verbose_name_plural = 'problem'
        indexes = [
            models.Index(fields=['problem']),
            models.Index(fields=['type']),
        ]
    def to_json(self):
        return {
            "problem":self.problem,
            "type":self.type
        }

class Submission(models.Model):
    """
    在线判题系统的提交记录模型
    """
    
    # 提交用户ID (关联用户系统)
    real_id=models.IntegerField(verbose_name="submission_id",unique=True)
    # 提交时间 (自动记录创建时间)
    time = models.DateTimeField(auto_now_add=True, verbose_name='time')
    problem=models.CharField(max_length=20,default="P1000",verbose_name="problem")
    # 提交内容 (JSON格式存储代码和语言等信息)
    body = models.JSONField(verbose_name='body')
    # 判题状态 (选择状态)
    status = models.CharField(
        max_length=30,
        default='WJ',
        verbose_name='status'
    )
    # 得分 (0-100分)
    score = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='score'
    )
    # 判题记录详情 (JSON格式存储测试用例结果等)
    record = models.JSONField(
        null=True,
        blank=True,
        verbose_name='record'
    )
    class Meta:
        verbose_name = 'submission'
        verbose_name_plural = 'submission'
        indexes = [
            models.Index(fields=['real_id']),
            models.Index(fields=['status']),
            models.Index(fields=['problem']),
        ]
